package com.ymm.controller;

import com.baomidou.mybatisplus.annotation.TableId;
import com.ymm.domain.entity.Menu;
import com.ymm.domain.entity.RoleMenu;
import com.ymm.domain.vo.AllMenuVo;
import com.ymm.domain.vo.ResponseResult;
import com.ymm.enums.AppHttpCodeEnum;
import com.ymm.service.MenuService;
import com.ymm.service.RoleMenuService;
import com.ymm.utils.BeanCopyUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author 椰咩咩咩
 * @version 1.0-SNAPSHOT
 * @date 2023/4/3 4:08
 * @Description
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private RoleMenuService roleMenuService;

    @Resource
    private MenuService menuService;

    @PreAuthorize("@ps.hasPermission('sys:menu:no')")
    @GetMapping("/no/{id}")
    public ResponseResult roleNoPerm(@PathVariable Long id) {
        return menuService.roleNoPerm(id);
    }

    @PreAuthorize("@ps.hasPermission('sys:menu:has')")
    @GetMapping("/has/{id}")
    public ResponseResult getMenusByRole(@PathVariable Long id) {
        return menuService.getMenusByRole(id);
    }

    @PreAuthorize("@ps.hasPermission('sys:menu:role:del')")
    @DeleteMapping("/role/del")
    public ResponseResult delMenuByRole(@RequestBody RoleMenu roleMenu) {
        roleMenuService.delMenuByRole(roleMenu);
        return ResponseResult.okResult();
    }

    @PreAuthorize("@ps.hasPermission('sys:menu:list')")
    @GetMapping("/page")
    public ResponseResult pageMenus(
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(required = false) String label) {
        return menuService.pageMenus(pageNum, pageSize, label);
    }

    @PreAuthorize("@ps.hasPermission('sys:menu:all')")
    @GetMapping("/all")
    public ResponseResult allMenus() {
        List<Menu> menus = menuService.list();
        List<AllMenuVo> menuVos = BeanCopyUtils.copyBeanList(menus, AllMenuVo.class);
        return ResponseResult.okResult(menuVos);
    }

    @PreAuthorize("@ps.hasPermission('sys:menu:add')")
    @PostMapping
    public ResponseResult addMenu(@RequestBody Menu menu) {
        boolean b = menuService.save(menu);
        return b ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.ADD_FAIL);
    }

    @PreAuthorize("@ps.hasPermission('sys:menu:update')")
    @PutMapping
    public ResponseResult updateMenu(@RequestBody Menu menu) {
        boolean b = menuService.updateById(menu);
        return b ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_FAIL);
    }

    @PreAuthorize("@ps.hasPermission('sys:menu:delete')")
    @DeleteMapping("/{id}")
    public ResponseResult deleteMenu(@PathVariable Long id) {
        boolean b = menuService.removeById(id);
        return b ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_FAIL);
    }

}
